package com.axend.aerosense.dev.fragment;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.ViewModelProvider;

import com.axend.aerosense.base.fragment.MvvmBaseFragment;
import com.axend.aerosense.base.impl.ILoadDataResult;
import com.axend.aerosense.base.impl.IOperationResult;
import com.axend.aerosense.base.livedatabus.LiveDatabus;
import com.axend.aerosense.base.viewmodel.BaseViewModelFactroy;
import com.axend.aerosense.common.base.IHostLayout;
import com.axend.aerosense.common.base.ITitle;
import com.axend.aerosense.common.bean.RadarBean;
import com.axend.aerosense.common.bean.RadarType;
import com.axend.aerosense.common.config.Gobal;
import com.axend.aerosense.common.ui.CustomInputDialog;
import com.axend.aerosense.common.ui.CustomSettingItemView;
import com.axend.aerosense.common.ui.CustomTipsDialog;
import com.axend.aerosense.common.ui.CustomTitleTipsDialog;
import com.axend.aerosense.common.util.FragmentUtil;
import com.axend.aerosense.dev.BR;
import com.axend.aerosense.dev.R;
import com.axend.aerosense.dev.databinding.DevFragmentDeviceSettingsBinding;
import com.axend.aerosense.dev.fragment.checkupdate.DevCheckUpdateFragment;
import com.axend.aerosense.dev.viewmodel.RadarSettingViewModel;
import com.blankj.utilcode.util.ToastUtils;

/**
 * 设备设置
 *
 * @author ChenHongWei
 * @date :2021/1/7 11:25
 */

public class DevDeviceSettingsFragment
        extends MvvmBaseFragment<DevFragmentDeviceSettingsBinding, RadarSettingViewModel>
        implements ILoadDataResult<RadarBean> {

    public final static String TARGET_RESET_PARAMETER = "TARGET_RESET_PARAMETER";
    public final static String ARG_PARAM = "RadarBean";
    public final static String RADAR_ID = "radarID";
    public final static String RADAR_STATE = "radarState";
    public final static String RE_BIND = "Re_Bind";
    private RadarBean radarBean;
    private CustomInputDialog customInputDialog = null;

    public static DevDeviceSettingsFragment getInstance(RadarBean radarBean) {
        DevDeviceSettingsFragment fragment = new DevDeviceSettingsFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(ARG_PARAM, radarBean);
        fragment.setArguments(bundle);   //设置参数
        return fragment;
    }

    @Override
    protected void initParameters() {
        super.initParameters();
        Bundle bundle = getArguments();
        if (null != bundle) {
            radarBean = (RadarBean) bundle.getSerializable(ARG_PARAM);
        }
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init();

    }

    private void init() {
        //呼吸与心率 雷达校准
        viewDataBinding.devCalibration.setOnClickListener((CustomSettingItemView.OnClickListener) v -> {
            Fragment fragment = DevBedCalibrationFragment.getInstance();
            Bundle bundle = new Bundle();
            bundle.putString(RADAR_ID, radarBean.getRadarUuid());
            bundle.putInt(RADAR_STATE, radarBean.getRadarState());
            fragment.setArguments(bundle);
            FragmentUtil.add(getActivity(), ((IHostLayout) getActivity()).getContentId(), fragment, this, true);

        });
//        //设备状态-呼吸与心率
//        viewDataBinding.devDeviceStatus.setOnClickListener((CustomSettingItemView.OnClickListener) v -> {
//            Fragment fragment = DevBedAndBreatheFragment.getInstance(radarBean);
//            FragmentUtil.add(getActivity(), ((IHostLayout) getActivity()).getContentId(), fragment, this, true);
//
//        });
        //重新绑定房间
        viewDataBinding.devAssignRoom.setOnClickListener((CustomSettingItemView.OnClickListener) view -> {
            Fragment fragment = DevBindRoomFragment.getInstance();
            Bundle bundle = new Bundle();
            bundle.putBoolean(RE_BIND, true);
            bundle.putSerializable(ARG_PARAM, radarBean);
            fragment.setArguments(bundle);
            FragmentUtil.add(getActivity(), ((IHostLayout)getActivity()).getContentId(), fragment, this, true);
        });

        viewDataBinding.devHardwareVersion.setOnClickListener((CustomSettingItemView.OnClickListener) view -> {
            ClipboardManager clip = (ClipboardManager)getContext().getSystemService(Context.CLIPBOARD_SERVICE);
            ClipData clipData = ClipData.newPlainText("RadarId", radarBean.getRadarUuid());
            clip.setPrimaryClip(clipData);
            ToastUtils.showShort(R.string.copying_serial_tips);
        });

        //删除按钮
        viewDataBinding.devDelete.setOnClickListener(v -> {
            CustomTipsDialog tipsDialog = new CustomTipsDialog(getContext(), getString(R.string.dev_delete_device_tips));
            tipsDialog.setOnClickBottomListener(new CustomTipsDialog.OnClickBottomListener() {
                @Override
                public void onPositiveClick() {
                    tipsDialog.dismiss();
                    viewModel.deleteRadar(getActivity(), new IOperationResult() {
                        @Override
                        public void onSuccess() {
                            // 通知数据变更
                            LiveDatabus.getInstance().with(Gobal.EVENT_KEY.DEV_COUNT_CHANGE, Integer.class).postValue(1);
                            LiveDatabus.getInstance().with(Gobal.EVENT_KEY.REFESH_ROOM, Integer.class).postValue(1);
                            getActivity().finish();
                        }

                        @Override
                        public void onFail() {

                        }
                    });
                }

                @Override
                public void onNegtiveClick() {
                    tipsDialog.dismiss();
                }
            });
            tipsDialog.show();
        });
        //设备名称
        viewDataBinding.devDeviceName.setOnClickListener((CustomSettingItemView.OnClickListener) view -> {
            customInputDialog = new CustomInputDialog(getActivity(), getString(R.string.dev_device_name_dialog));
            customInputDialog.setOnClickBottomListener(new CustomInputDialog.OnClickBottomListener() {
                @Override
                public void onPositiveClick() {
                    String name = customInputDialog.getInputValue();
                    if (TextUtils.isEmpty(name)) {
                        return;
                    }
                    viewModel.updataName(getActivity(), name, new IOperationResult() {

                        @Override
                        public void onSuccess() {
                            customInputDialog.dismiss();
                            LiveDatabus.getInstance().with(Gobal.EVENT_KEY.DEV_COUNT_CHANGE, Integer.class).postValue(1);
                        }

                        @Override
                        public void onFail() {

                        }
                    });
                    customInputDialog.dismiss();
                }

                @Override
                public void onNegtiveClick() {
                    customInputDialog.dismiss();
                }
            });
            customInputDialog.show();
        });

        //检查更新
        viewDataBinding.devCheckUpdate.setOnClickListener(v -> {
            Fragment fragment = DevCheckUpdateFragment.getInstance();
            Bundle bundle = new Bundle();
            bundle.putString(RADAR_ID, radarBean.getRadarUuid());
            fragment.setArguments(bundle);
            FragmentUtil.add(getActivity(), ((IHostLayout) getActivity()).getContentId(), fragment, this, true);
        });
        //修改参数
        viewDataBinding.devResetParameters.setOnClickListener((CustomSettingItemView.OnClickListener) v -> {
            toRestParametersPager();
        });

        LiveDatabus.getInstance().with(Gobal.EVENT_KEY.DEV_COUNT_CHANGE, Integer.class).observe(getViewLifecycleOwner(), integer -> {
            onRetryBtnClick();
        });

        viewDataBinding.devWavveProConfig.setOnClickListener(v->{
            Fragment fragment = DevWavveProInstallConfigFragment.getInstance(radarBean);
            FragmentUtil.add(getActivity(), ((IHostLayout) getActivity()).getContentId(), fragment, this, true);
        });

        setLoadSir(viewDataBinding.devContentView);
        showLoading();
        viewModel.onLoad();
    }

    private void toRestParametersPager() {
        Bundle bundle = new Bundle();
        bundle.putSerializable(ARG_PARAM, radarBean);
        Fragment fragment;
        if (radarBean.getType() == RadarType.ASSURE){
            fragment = DevAssureSettingFragment.getInstance();

        } else if(radarBean.getType() == RadarType.ASSURE_LITE){
            fragment = DevWavveProInstallConfigFragment.getInstance();

        } else {
            fragment = DevWavveSettingFragment.getInstance();
        }
        fragment.setArguments(bundle);
        FragmentUtil.add(getActivity(),  ((IHostLayout)getActivity()).getContentId(),fragment, this, true);
    }

    @Override
    public void onResume() {
        super.onResume();
        ((ITitle) getActivity()).setTitle(getString(R.string.dev_device_settings));
        viewModel.getInstallTips(radarBean.getRadarUuid());

    }

    @Override
    public int getLayoutId() {
        return R.layout.dev_fragment_device_settings;
    }

    @Override
    public int getBindingVariable() {
        return BR.data;
    }

    @Override
    protected RadarSettingViewModel getViewModel() {
        BaseViewModelFactroy<RadarBean> factroy = new BaseViewModelFactroy<RadarBean>(this, this);
        RadarSettingViewModel tmp = new ViewModelProvider(this, factroy).get(RadarSettingViewModel.class);
        tmp.setValue(radarBean);
        return tmp;
    }

    @Override
    protected void onRetryBtnClick() {
        showLoading();
        viewModel.onLoad();
    }

    @Override
    public void loadDataSuccess(RadarBean radarBean) {
//        if (radarBean.getRadarState() == 1) {
//            //雷达离线，提示用户重设参数
//            CustomTitleTipsDialog dialog = new CustomTitleTipsDialog(getContext());
//            dialog.setTips(getString(R.string.dev_device_online_tips))
//                    .setOnClickBottomListener(new CustomTitleTipsDialog.OnClickBottomListener() {
//                        @Override
//                        public void onPositiveClick() {
//                            dialog.dismiss();
//                            toRestParametersPager();
//                        }
//
//                        @Override
//                        public void onNegtiveClick() {
//                            dialog.dismiss();
//                        }
//                    })
//                    .show();
//        }
        viewModel.setValue(radarBean);

    }

    @Override
    public void loadDataFail(String decs) {

    }
}
